<template>
  <el-dialog
    :title="!dataForm.oldHouseId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="区域" prop="oldHouseArea">
      <el-input v-model="dataForm.oldHouseArea" placeholder="区域"></el-input>
    </el-form-item>
    <el-form-item label="二手房总价" prop="oldHousePrice">
      <el-input v-model="dataForm.oldHousePrice" placeholder="二手房总价"></el-input>
    </el-form-item>
    <el-form-item label="二手房户型（0:一居室 1:二居室 2:三居室 3:四居室 4:五居室 5:五居室以上）" prop="oldHouseType">
      <el-input v-model="dataForm.oldHouseType" placeholder="二手房户型（0:一居室 1:二居室 2:三居室 3:四居室 4:五居室 5:五居室以上）"></el-input>
    </el-form-item>
    <el-form-item label="面积" prop="oldHouseAcreage">
      <el-input v-model="dataForm.oldHouseAcreage" placeholder="面积"></el-input>
    </el-form-item>
    <el-form-item label="租房类型 (0. 合租 1:整租 )" prop="oldHouseRentingType">
      <el-input v-model="dataForm.oldHouseRentingType" placeholder="租房类型 (0. 合租 1:整租 )"></el-input>
    </el-form-item>
    <el-form-item label="租金" prop="oldHouseRentingPrice">
      <el-input v-model="dataForm.oldHouseRentingPrice" placeholder="租金"></el-input>
    </el-form-item>
    <el-form-item label="房屋类型 (0:二手房 1:租房)" prop="oldHomeType">
      <el-input v-model="dataForm.oldHomeType" placeholder="房屋类型 (0:二手房 1:租房)"></el-input>
    </el-form-item>
    <el-form-item label="房屋朝向 (0:东 1:南 2:西 3:北 )" prop="oldHouseOrientation">
      <el-input v-model="dataForm.oldHouseOrientation" placeholder="房屋朝向 (0:东 1:南 2:西 3:北 )"></el-input>
    </el-form-item>
    <el-form-item label="房屋标题" prop="oldHouseTitle">
      <el-input v-model="dataForm.oldHouseTitle" placeholder="房屋标题"></el-input>
    </el-form-item>
    <el-form-item label="房屋描述" prop="oldHouseDescription">
      <el-input v-model="dataForm.oldHouseDescription" placeholder="房屋描述"></el-input>
    </el-form-item>
    <el-form-item label="房屋位置" prop="oldHouseSite">
      <el-input v-model="dataForm.oldHouseSite" placeholder="房屋位置"></el-input>
    </el-form-item>
    <el-form-item label="楼层" prop="oldHouseFloorLevel">
      <el-input v-model="dataForm.oldHouseFloorLevel" placeholder="楼层"></el-input>
    </el-form-item>
    <el-form-item label="电梯" prop="oldHouseElevator">
      <el-input v-model="dataForm.oldHouseElevator" placeholder="电梯"></el-input>
    </el-form-item>
    <el-form-item label="年代" prop="oldHouseYear">
      <el-input v-model="dataForm.oldHouseYear" placeholder="年代"></el-input>
    </el-form-item>
    <el-form-item label="经度" prop="oldHouseLongitude">
      <el-input v-model="dataForm.oldHouseLongitude" placeholder="经度"></el-input>
    </el-form-item>
    <el-form-item label="纬度" prop="oldHouseLatitude">
      <el-input v-model="dataForm.oldHouseLatitude" placeholder="纬度"></el-input>
    </el-form-item>
    <el-form-item label="用户id" prop="oldHouseUserId">
      <el-input v-model="dataForm.oldHouseUserId" placeholder="用户id"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          oldHouseId: 0,
          oldHouseArea: '',
          oldHousePrice: '',
          oldHouseType: '',
          oldHouseAcreage: '',
          oldHouseRentingType: '',
          oldHouseRentingPrice: '',
          oldHomeType: '',
          oldHouseOrientation: '',
          oldHouseTitle: '',
          oldHouseDescription: '',
          oldHouseSite: '',
          oldHouseFloorLevel: '',
          oldHouseElevator: '',
          oldHouseYear: '',
          oldHouseLongitude: '',
          oldHouseLatitude: '',
          oldHouseUserId: ''
        },
        dataRule: {
          oldHouseArea: [
            { required: true, message: '区域不能为空', trigger: 'blur' }
          ],
          oldHousePrice: [
            { required: true, message: '二手房总价不能为空', trigger: 'blur' }
          ],
          oldHouseType: [
            { required: true, message: '二手房户型（0:一居室 1:二居室 2:三居室 3:四居室 4:五居室 5:五居室以上）不能为空', trigger: 'blur' }
          ],
          oldHouseAcreage: [
            { required: true, message: '面积不能为空', trigger: 'blur' }
          ],
          oldHouseRentingType: [
            { required: true, message: '租房类型 (0. 合租 1:整租 )不能为空', trigger: 'blur' }
          ],
          oldHouseRentingPrice: [
            { required: true, message: '租金不能为空', trigger: 'blur' }
          ],
          oldHomeType: [
            { required: true, message: '房屋类型 (0:二手房 1:租房)不能为空', trigger: 'blur' }
          ],
          oldHouseOrientation: [
            { required: true, message: '房屋朝向 (0:东 1:南 2:西 3:北 )不能为空', trigger: 'blur' }
          ],
          oldHouseTitle: [
            { required: true, message: '房屋标题不能为空', trigger: 'blur' }
          ],
          oldHouseDescription: [
            { required: true, message: '房屋描述不能为空', trigger: 'blur' }
          ],
          oldHouseSite: [
            { required: true, message: '房屋位置不能为空', trigger: 'blur' }
          ],
          oldHouseFloorLevel: [
            { required: true, message: '楼层不能为空', trigger: 'blur' }
          ],
          oldHouseElevator: [
            { required: true, message: '电梯不能为空', trigger: 'blur' }
          ],
          oldHouseYear: [
            { required: true, message: '年代不能为空', trigger: 'blur' }
          ],
          oldHouseLongitude: [
            { required: true, message: '经度不能为空', trigger: 'blur' }
          ],
          oldHouseLatitude: [
            { required: true, message: '纬度不能为空', trigger: 'blur' }
          ],
          oldHouseUserId: [
            { required: true, message: '用户id不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.oldHouseId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.oldHouseId) {
            this.$http({
              url: this.$http.adornUrl(`/oldhouse/oldhouse/info/${this.dataForm.oldHouseId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.oldHouseArea = data.oldHouse.oldHouseArea
                this.dataForm.oldHousePrice = data.oldHouse.oldHousePrice
                this.dataForm.oldHouseType = data.oldHouse.oldHouseType
                this.dataForm.oldHouseAcreage = data.oldHouse.oldHouseAcreage
                this.dataForm.oldHouseRentingType = data.oldHouse.oldHouseRentingType
                this.dataForm.oldHouseRentingPrice = data.oldHouse.oldHouseRentingPrice
                this.dataForm.oldHomeType = data.oldHouse.oldHomeType
                this.dataForm.oldHouseOrientation = data.oldHouse.oldHouseOrientation
                this.dataForm.oldHouseTitle = data.oldHouse.oldHouseTitle
                this.dataForm.oldHouseDescription = data.oldHouse.oldHouseDescription
                this.dataForm.oldHouseSite = data.oldHouse.oldHouseSite
                this.dataForm.oldHouseFloorLevel = data.oldHouse.oldHouseFloorLevel
                this.dataForm.oldHouseElevator = data.oldHouse.oldHouseElevator
                this.dataForm.oldHouseYear = data.oldHouse.oldHouseYear
                this.dataForm.oldHouseLongitude = data.oldHouse.oldHouseLongitude
                this.dataForm.oldHouseLatitude = data.oldHouse.oldHouseLatitude
                this.dataForm.oldHouseUserId = data.oldHouse.oldHouseUserId
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/oldhouse/oldhouse/${!this.dataForm.oldHouseId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'oldHouseId': this.dataForm.oldHouseId || undefined,
                'oldHouseArea': this.dataForm.oldHouseArea,
                'oldHousePrice': this.dataForm.oldHousePrice,
                'oldHouseType': this.dataForm.oldHouseType,
                'oldHouseAcreage': this.dataForm.oldHouseAcreage,
                'oldHouseRentingType': this.dataForm.oldHouseRentingType,
                'oldHouseRentingPrice': this.dataForm.oldHouseRentingPrice,
                'oldHomeType': this.dataForm.oldHomeType,
                'oldHouseOrientation': this.dataForm.oldHouseOrientation,
                'oldHouseTitle': this.dataForm.oldHouseTitle,
                'oldHouseDescription': this.dataForm.oldHouseDescription,
                'oldHouseSite': this.dataForm.oldHouseSite,
                'oldHouseFloorLevel': this.dataForm.oldHouseFloorLevel,
                'oldHouseElevator': this.dataForm.oldHouseElevator,
                'oldHouseYear': this.dataForm.oldHouseYear,
                'oldHouseLongitude': this.dataForm.oldHouseLongitude,
                'oldHouseLatitude': this.dataForm.oldHouseLatitude,
                'oldHouseUserId': this.dataForm.oldHouseUserId
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
